package cn.xzchain.concurrency.singleton;

import cn.xzchain.concurrency.annoations.Recommend;
import cn.xzchain.concurrency.annoations.ThreadSafe;

/**
 * @author lihang
 * @date 2018/5/7.
 * @description 懒汉模式，借助枚举（借助静态内部类也相似见8）
 */
@ThreadSafe
@Recommend
public class Singleton7 {

    private Singleton7(){
    }


    public static Singleton7 getInstance(){
        return Singleton.INSTANCE.getSingleton7();
    }

    private enum Singleton{
        INSTANCE;

        private Singleton7 singleton7;

        //jvm保证这个方法只会调用一次
        Singleton(){
            singleton7 = new Singleton7();
        }

        public Singleton7 getSingleton7(){
            return singleton7;
        }
    }
}
